package top.boycharse.blog.controller.manager;

import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.boycharse.blog.bean.Response.BaseResponse;
import top.boycharse.blog.bean.Response.ClassifyResponse;
import top.boycharse.blog.bean.Response.ResponseCode;
import top.boycharse.blog.bean.request.ClassifyInfo;
import top.boycharse.blog.dao.bean.Classify;
import top.boycharse.blog.dao.service.ClassifyService;
import top.boycharse.blog.exception.ParameterException;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/manager/classify")
public class ManagerClassifyController {

    public static final String CLASSIFY_IS_EXIT = "该分类已存在！";

    public static final String CLASSIFY_NOT_EXIT = "不存在该分类！";

    public static final String CLASSIFY_DELETE_SUCCESS = "删除成功！";

    @Autowired
    private ClassifyService classifyService;

    /**
     * 获取所有的分类
     * @return
     */
    @GetMapping("/getClassify")
    public BaseResponse<?> getClassify() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<Classify> classifies = classifyService.getAllClassify(username);
        List<ClassifyResponse> responses = new ArrayList<>();
        for (Classify temp:classifies) {
            responses.add(new ClassifyResponse(temp));
        }
        return new BaseResponse<>(responses);
    }

    /**
     * 创建分类
     * @return ClassifyResponse
     */
    @PostMapping("/createClassify")
    public BaseResponse<?> createClassify(@RequestBody ClassifyInfo info){
        if (StringUtils.isEmpty(info.getName())) {
            return BaseResponse.buildParamException();
        }

        Classify classify = classifyService.findByName(info.getName());

        if (classify != null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL, CLASSIFY_IS_EXIT);
        }

        classify = new Classify(info);

        // 获取当前用户名
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        top.boycharse.blog.dao.bean.User user = new top.boycharse.blog.dao.bean.User();
        user.setUserName(userName);
        classify.setUser(user);
        classify = classifyService.saveClassify(classify);
        ClassifyResponse response = new ClassifyResponse(classify);

        return new BaseResponse<>(response);
    }

    /**
     * 删除标签
     * @param id 标签的id
     * @return BaseResponse
     */
    @PostMapping("/deleteClassify")
    public BaseResponse<?> deleteClassify(@RequestParam("id")Integer id) {
        Classify classify = classifyService.findById(id);
        if (classify == null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,CLASSIFY_NOT_EXIT);
        }else {
            classifyService.deleteById(id);
            return BaseResponse.buildSuccessMsg(CLASSIFY_DELETE_SUCCESS);
        }
    }

    /**
     * 更新标签名
     * @param info
     * @return
     */
    @PostMapping("/updateClassify")
    public BaseResponse<?> updateClassify(@RequestBody ClassifyInfo info) {
        if (StringUtils.isEmpty(info.getName())) {
            return BaseResponse.buildParamException();
        }
        Classify classify = classifyService.findById(info.getId());
        if (classify == null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,CLASSIFY_NOT_EXIT);
        }
        Classify temp = classifyService.findByName(info.getName());
        if (temp != null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,CLASSIFY_IS_EXIT);
        }

        classifyService.update(classify, info.getName());

        ClassifyResponse response = new ClassifyResponse(classify);

        return new BaseResponse<>(response);
    }
}
